#include <event2/event.h>
#include <stdio.h>
/* Change the callback and callback_arg of ’ev’, which must not be pending.*/
int replace_callback(struct event* ev, event_callback_fn new_callback,void* new_callback_arg)
{
    struct event_base* base;
    evutil_socket_t fd;
    short events;
    int pending;
    pending = event_pending(ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT,NULL);
    if (pending) {
    /* We want to catch this here so that we do not re-assign a
    *pending event. That would be very very bad.*/
        fprintf(stderr,"Error! replace_callback called on a pending event!\n");
        return -1;
    }
    event_get_assignment(ev, &base, &fd, &events,
                                NULL /* ignore old callback*/ ,
                                NULL /* ignore old callback argument*/);
    event_assign(ev, base, fd, events, new_callback, new_callback_arg);
    return 0;
}
void cb_func(evutil_socket_t fd, short what, void* arg)
{
    const char* data = arg;
    printf("Got an event on socket %d:%s%s%s%s [%s]\n",
        (int) fd,
        (what&EV_TIMEOUT) ? " timeout" : "",
        (what&EV_READ) ? " read" : "",
        (what&EV_WRITE) ? " write" : "",
        (what&EV_SIGNAL) ? " signal" : "",
        data);
}
void main_loop(evutil_socket_t fd1, evutil_socket_t fd2)
{
    struct event* ev1;//, * ev2;
    struct timeval five_seconds = {5,0};
    struct event_base* base = event_base_new();
    /* The caller has already set up fd1, fd2 somehow, and make them nonblocking.*/
    ev1 = event_new(base, fd1, EV_TIMEOUT|EV_READ, cb_func,(char * )"Reading event");
    //ev2 = event_new(base, fd2, EV_WRITE|EV_PERSIST, cb_func,(char * )"Writing event");
    event_add(ev1, &five_seconds);
    //event_add(ev2, NULL);
    event_base_dispatch(base);

    replace_callback(ev1,cb_func,"replace_callback");
    event_add(ev1, &five_seconds);
    event_base_dispatch(base);
}
int main()
{
    main_loop(fileno(stdin),fileno(stdin));
    return 0;
}